/**
 * created by 小卷毛, 2021-07-29
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import androidx.lifecycle.MutableLiveData
import com.google.android.material.tabs.TabLayout
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.RxSingleHelper
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.lzy.okgo.cache.CacheMode
import com.lzy.okgo.model.HttpMethod
import com.rzico.sbl.R
import com.rzico.sbl.model.*
import com.xinnuo.common.extend.find
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.applySchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally
import com.xinnuo.common_ui.view.BadgeTextView

/**
 * 描 述：
 * 作 者：小卷毛  2021-07-29 15:52
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class MasterViewModel : DialogViewModel() {

    val mHandleEvent = MutableLiveData("{}")

    val mRoleList by lazy {
        listOf(
            FilterData("管理员", "1", "1"),
            FilterData("店长", "2", "1,2"),
            FilterData("客服", "10", "1,2,10"),
            FilterData("送货员", "3", "1,2,3,10"),
            FilterData("推广员", "4", "1,2,3,4,10"),
        )
    }

    /**
     * 员工列表
     */
    fun masterList(
        page: Int,
        onSuccess: (ArrayList<MasterData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseModel<MasterData>>>(BaseUrl.mngList)
            .params(
                getParams(
                    "pageStart" to page * 10,
                    "pageSize" to 10
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<MasterData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 员工指派列表
     */
    fun masterNamedList(
        page: Int,
        keyWord: String,
        masterId: String,
        onSuccess: (ArrayList<MemberData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<MemberData>>>(BaseUrl.mngNList)
            .params(
                getParams(
                    "searchValue" to keyWord,
                    "adminId" to masterId,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<MemberData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 转移记录列表
     */
    fun transferList(
        page: Int,
        onSuccess: (ArrayList<MasterTransferData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<MasterTransferData>>>(BaseUrl.mngTransRcd)
            .params(
                getParams(
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<MasterTransferData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 删除员工
     */
    fun masterDel(itemId: String) =
        RxSingleHelper.requestString(
            url = BaseUrl.mngDel,
            params = getParams("id" to itemId)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    /**
     * 员工店铺列表
     */
    fun masterShop(
        isLoading: Boolean = true,
        requestUrl: String = BaseUrl.mngShop
    ) =
        OkGo.get<BaseResponse<ResponseModel<MasterData>>>(requestUrl)
            .params(
                getParams(
                    "pageStart" to 0,
                    "pageSize" to 500
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<MasterData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { if (isLoading) showLoading() }
            .doFinally { if (isLoading) dismissLoading() }
            .map { it.data.data }

    /**
     * 员工位置列表
     */
    fun masterLocation() =
        OkGo.get<BaseResponse<ArrayList<MasterData>>>(BaseUrl.mngLocation)
            .cacheMode(CacheMode.NO_CACHE)
            .retryCount(0)
            .converter(object : JacksonConvert<BaseResponse<ArrayList<MasterData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }

    /**
     * 获取押桶列表
     */
    fun handleList(
        page: Int,
        keyWord: String,
        beginDate: String,
        endDate: String,
        onSuccess: (ArrayList<HandleData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseModel<HandleData>>>(BaseUrl.mngBucket)
            .params(
                getParams(
                    "searchValue" to keyWord,
                    "beginDate" to beginDate,
                    "endDate" to endDate,
                    "pageStart" to page * 10,
                    "pageSize" to 10
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<HandleData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 获取押桶合计
     */
    fun handleTotal(
        keyWord: String,
        beginDate: String,
        endDate: String
    ) =
        request(
            isLoadingBefore = false,
            isLoadingAfter = false,
            requestUrl = BaseUrl.mngBucketSum,
            requestMethod = HttpMethod.GET,
            params = arrayOf(
                "searchValue" to keyWord,
                "beginDate" to beginDate,
                "endDate" to endDate
            ),
            onSuccess = { mHandleEvent.value = it }
        )

    /**
     * 获取退桶列表
     */
    fun barrelList(
        page: Int,
        state: String,
        keyWord: String,
        onSuccess: (ArrayList<BarrelData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.post<BaseResponse<BarrelModel>>(BaseUrl.mngBarrel)
            .isMultipart(true)
            .params(
                getParams(
                    "status" to state,
                    "searchValue" to keyWord,
                    "pageStart" to page * 10,
                    "pageSize" to 10
                )
            )
            .converter(object : JacksonConvert<BaseResponse<BarrelModel>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 退桶单数量
     */
    fun barrelCount() =
        RxSingleHelper
            .requestString(url = BaseUrl.mngBarrelCut)
            .compose(applySchedulers())
            .doOnSubscribe { }
            .doFinally { }

    /**
     * 更新自定义Tab数量
     */
    fun updateNum(
        unconfirmed: String,
        dispatch: String,
        delivery: String,
        layout: TabLayout
    ) {
        layout.getTabAt(0)?.let { tab ->
            tab.customView
                ?.find<BadgeTextView>(R.id.item_tab_num)
                ?.setBadgeCount(unconfirmed)
        }
        layout.getTabAt(1)?.let { tab ->
            tab.customView
                ?.find<BadgeTextView>(R.id.item_tab_num)
                ?.setBadgeCount(dispatch)
        }
        layout.getTabAt(2)?.let { tab ->
            tab.customView
                ?.find<BadgeTextView>(R.id.item_tab_num)
                ?.setBadgeCount(delivery)
        }
    }

}